package org.test.client.ui.skinui.txtfield;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.border.LineBorder;
import javax.swing.plaf.InsetsUIResource;
import javax.swing.plaf.basic.BasicTextFieldUI;

import org.jb2011.ninepatch4j.NinePatch;

/**
 * 对某一个 jtextField 执行setUI 即可应用 此UI。
 * 
 * 设置组件的边框和它的文本之间的空白。文本组件的默认 Border 对象将使用此值创建适当的空白。
 * 不过，如果在文本组件上设置非默认边框则由 Border 对象负责创建适当的空白（其他情况下实际上忽略此属性）。
 * 这会导致重绘该组件。PropertyChange 事件 ("margin") 被发送到所有侦听器。
 * 
 * @author guodeqi
 *
 */
public class MyTextFieldUI extends BasicTextFieldUI{
	private static String normalPic = "res/bg_login_text_normal.9.png";
	private static NinePatch ninePatchNormal = null;
	private static String disablePic = "res/bg_login_text_disable.9.png";
	private static NinePatch ninePatchDisable = null;
	private static String fucusPic = "res/bg_login_text_pressed.9.png";
	private static NinePatch ninePatchFucus = null;
	private static Insets margin = new InsetsUIResource(4,5,4,5);
	static{
		try {
			ninePatchNormal = NinePatch.load(MyTextFieldUI.class.getResourceAsStream(normalPic), true, false);
			ninePatchDisable = NinePatch.load(MyTextFieldUI.class.getResourceAsStream(disablePic), true, false);
			ninePatchFucus = NinePatch.load(MyTextFieldUI.class.getResourceAsStream(fucusPic), true, false);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	private JComponent tmpComp = null;
	private NinePatch npPointer = null;
	
    public static MyTextFieldUI createUI(JComponent c) 
    {
    	MyTextFieldUI myTextFieldUI = new MyTextFieldUI();
    	myTextFieldUI.tmpComp = c;
    	myTextFieldUI.Init();
    	
        return myTextFieldUI;
    }

	private MyTextFieldUI(){}

	public void Init() {
		if(tmpComp.isEnabled()){
			npPointer = ninePatchNormal;
		}else{
			npPointer = ninePatchDisable;
		}
		tmpComp.setBorder(new TranslucentBorder());
		tmpComp.addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent focusevent) {
				npPointer = ninePatchNormal;
				tmpComp.repaint();
			}
			
			public void focusGained(FocusEvent focusevent) {
				npPointer = ninePatchFucus;
				tmpComp.repaint();
			}
		});
	}
	
	protected void paintBackground(Graphics g) {
//		此处 设置边框为null 的话 则margin 无法控制
//		必须绘制 透明边框 并返回内部 insets
//		tmpComp.setBorder(null);
		
	  	//先调用父类方法把背景刷新下（比如本UI里使用的大圆角NP图如不先刷新背景则会因上下拉动滚动条
    	//而致4个圆角位置得不到刷新，从而影响视觉效果（边角有前面的遗留），置于透明边角不被透明像素填
    	//充的问题，它有可能是Android的NinePatch技术为了性能做作出的优化——一切全透明像素即意味着不需绘制）
	 	super.paintBackground(g);
	 	
		Dimension psize = tmpComp.getPreferredSize();
		Dimension size = tmpComp.getSize();
		int w = Math.max(psize.width, size.width);
		int h = Math.max(psize.height, size.height);
		npPointer.draw((Graphics2D)g, 0, 0, w, h);
	}
	
	class TranslucentBorder extends LineBorder{
		public TranslucentBorder(){
			super(new Color(0,0,0,0));
		}
		
		public TranslucentBorder(Color color) {
			super(color);
		}
		
		@Override
		public Insets getBorderInsets(Component c) {
			return getBorderInsets(c, new Insets(0, 0, 0, 0));
		}
		
		@Override
		public Insets getBorderInsets(Component c, Insets insets) {
			return margin;
		}
	}
}
